package cn.tx.controller;


import cn.tx.dao.BooksDao;
import cn.tx.model.*;
import cn.tx.page.Page;
import cn.tx.service.BooksService;
import cn.tx.service.ClazzService;
import cn.tx.service.GradeService;
import cn.tx.util.StringUtil;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping(path = "/books")
public class BooksController {

    @Autowired
    private ClazzService clazzService;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private BooksService booksService;


    /*首页展示*/
    @RequestMapping(path = "/list")
    public ModelAndView list(ModelAndView modelAndView) {
        modelAndView.setViewName("books/books_list");
        List<Books>booksList = booksService.findAll();
        modelAndView.addObject("booksList",booksList);
        modelAndView.addObject("booksListJson", JSONArray.fromObject(booksList));
        return modelAndView;
    }
    /*展示书籍列表*/
    @RequestMapping(path = "/get_list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getList(
            @RequestParam(value="name",required=false,defaultValue="") String name,
            @RequestParam(value="author",required=false,defaultValue="") String author,
            Page page
    ) {
        Map<String,Object> map = new HashMap<String,Object>();
        Map<String,Object> querymap = new HashMap<String,Object>();
        if(name!=null){
            querymap.put("name","%"+name+"%");
        }

        if (author!=null){
            querymap.put("author","%"+author+"%");
        }
        querymap.put("offset", page.getOffset());
        querymap.put("pageSize", page.getRows());
        map.put("rows",booksService.findList(querymap));
        map.put("total",booksService.findTotal(querymap));
        return map;
    }
    /*添加书籍*/
    @RequestMapping(path = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Books books) {
        Map<String, String> map = new HashMap<>();
        if (books == null) {
            map.put("type", "error");
            map.put("msg", "添加失败，请联系管理员");
            return map;
        }

        if (StringUtils.isEmpty(books.getName())) {
            map.put("type", "error");
            map.put("msg", "书籍名不能为空");
            return map;
        }
        if (StringUtils.isEmpty(books.getAuthor())) {
            map.put("type", "error");
            map.put("msg", "作者不能为空");
            return map;
        }
        if (StringUtils.isEmpty(books.getContext())) {
            map.put("type", "error");
            map.put("msg", "信息不能为空");
            return map;
        }
        if (StringUtils.isEmpty(books.getTime())) {
            map.put("type", "error");
            map.put("msg", "信息不能为空");
            return map;
        }
        if (booksService.addBooks(books) <= 0) {
            map.put("type", "error");
            map.put("msg", "添加失败，请联系管理员");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "添加成功");
        return map;
    }
    /*删除书籍*/
    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(
            @RequestParam(value = "ids[]",required = true) Long[] ids
    ) {
        Map<String, String> map = new HashMap<>();
        if (ids==null||ids.length==0){
            map.put("type", "error");
            map.put("msg", "请至少选择一条数据");
            return map;
        }
        try {
            if(booksService.delete(StringUtil.joinString(Arrays.asList(ids), ",")) <= 0){
                map.put("type", "error");
                map.put("msg", "删除失败！");
                return map;
            }
        } catch (Exception e) {
            map.put("type", "error");
            map.put("msg", "确认删除！");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "删除成功");
        return map;
    }
    /*编辑书籍*/
    @RequestMapping(path = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(Books books) {
        Map<String, String> map = new HashMap<>();
        if (StringUtils.isEmpty(books.getName())) {
            map.put("type", "error");
            map.put("msg", "书名不能为空");
            return map;
        }
        if (StringUtils.isEmpty(books.getAuthor())) {
            map.put("type", "error");
            map.put("msg", "作者名不能为空");
            return map;
        }
        if (StringUtils.isEmpty(books.getContext())) {
            map.put("type", "error");
            map.put("msg", "信息不能为空");
            return map;
        }
        if (booksService.UpdateBooksById(books) <= 0) {
            map.put("type", "error");
            map.put("msg", "添加失败，请联系管理员");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "添加成功");
        return map;
    }
}
